//
//  AppDelegate.m
//  sheyingzu
//
//  Created by tyu on 28.5.24.
//

#import "AppDelegate.h"
#import "ZXZTabbarViewController.h"
#import "sheyingzu-Swift.h"
#import <AlipaySDK/AlipaySDK.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 开始网络监控
    [[XiaoSiRenLe UniqueInstanceIdentifier] BeginMonitoringNetworkStatus];
    
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    
    [ZXZNetworkRequest getLanchImageWithWindow:self.window];
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[XiaoSiRenLe UniqueInstanceIdentifier] StopMonitoringNetworkStatus];
}


- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if ([url.host isEqualToString:@"safepay"])
    {
        
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            //            NSLog(@"open url rest: %@",resultDic);
            if ([resultDic isKindOfClass:NSDictionary.class]){
                [self disposeAilyPayResultWith:resultDic];
            }else {
                [NotifyLabel NotifyWithString:@"支付出错"];
            }
        }];
    }
    return YES;
}

- (void)disposeAilyPayResultWith:(NSDictionary *)resultDic {
    int statusCode = [resultDic[@"resultStatus"]  intValue];
    if (statusCode == 9000) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ZXZAlipaySuccessNotification object:nil userInfo:nil];
    }else if (statusCode == 6001){
        // 用户中途取消
        NSString *memo = [NSString stringWithFormat:@"%@",resultDic[@"memo"]];
        memo = memo.length ? memo : @"支付失败";
        [NotifyLabel NotifyWithString:memo];
    }else {
        [NotifyLabel NotifyWithString:@"支付失败"];
    }
    
}



@end
